#include "TreeNode.h"
#include <vector>
#include <queue>
using namespace std;

vector<vector<int>> levelOrder(TreeNode* root) {
    vector<vector<int>> ret;
    if (root == nullptr) {
        return ret;
    }
    std::queue<TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        int level_size = q.size();
        vector<int> level;
        for (int i = 0; i<level_size; ++i) {
            TreeNode* peer = q.front();
            level.push_back(peer->val);
            if (peer->left) {
                q.push(peer->left);
            }
            if (peer->right) {
                q.push(peer->right);
            }
            q.pop();
        }
        ret.push_back(level);
    }
    return ret;
}